<?php

namespace Config;

use Phalcon\Mvc\Router;
use Phalcon\Mvc\Application;

class Route {

    private $application;

    public function __construct(Application $application) {
        if (is_a($application, "Phalcon\Mvc\Application"))
            $this->application = $application;
    }

    public function setRoute() {
        $config = require APP_PATH . '/config/config.php';
        $router = new Router();
        foreach ($this->application->getModules() as $key => $module) {
            $namespace = str_replace('Module', 'Controllers', $module["className"]);

            $router->add('/' . $key . '/:params', array(
                'namespace' => $namespace,
                'module' => $key,
                'controller' => 'Index',
                'action' => 'index',
                'params' => 1
            ))->setName($key);
            
            $router->add('/' . $key . '/:controller/:params', array(
                'namespace' => $namespace,
                'module' => $key,
                'controller' => 1,
                'action' => 'index',
                'params' => 2
            ));
            ;
            $router->add('/' . $key . '/:controller/:action/:params', array(
                'namespace' => $namespace,
                'module' => $key,
                'controller' => 1,
                'action' => 2,
                'params' => 3
            ));
        }
        $router->setDefaultModule($config->DefaultModule);
        $router->setDefaultNamespace($config->DefaultNamespace);
        $router->setDefaultController('Index');
        $router->setDefaultAction('index');
       
        $router->handle();
        return $router;
    }

}
